package ar.com.mindset.geometrics.colorSchemes
{
	import ar.com.mindset.geometrics.ColorEntry;
	import ar.com.mindset.geometrics.ColorScheme;
	import ar.com.mindset.geometrics.MapItem;
	import ar.com.mindset.geometrics.RegionData;
	import ar.com.mindset.geometrics.RegionsLegend;
	import ar.com.mindset.geometrics.legends.ListLegend;
	
	import com.degrafa.GeometryGroup;
	import com.degrafa.paint.SolidFill;
	
	import mx.collections.ArrayCollection;
	
	[DefaultProperty("discreteValues")]

	public class DiscreteColorScheme extends ColorScheme
	{
		
		public var legend:RegionsLegend = new ListLegend() ;

		public override function getLegend():GeometryGroup{
			return legend as GeometryGroup;
		}
		
		public var discreteValues:ArrayCollection = new ArrayCollection();
		
		public var defaultColor:Object= "#000000";
		
		public var name:String="Discrete values";
		
		public override function prepare(mapItem:MapItem, regionData:RegionData):void
		{
			var solidFill:SolidFill = new SolidFill()

			solidFill.color = getColor(regionData[valueProperty]);			

			mapItem.fillcolor = solidFill;
		}
		
		private function getColor(value:Object):Object{
			for each(var colorEntry:ColorEntry in discreteValues){
				if(colorEntry.key == value){
					return colorEntry.color;
				}
			}
			return defaultColor;
		}	
		
		public function toString():String{
			return name;
		}	
		
		public override function initialize(values:ArrayCollection):void {
			legend.initialize(discreteValues, defaultColor);
		}		
	}
}